package com.tj.bo.user.server;

import com.fqgj.base.services.redis.RedisClient;
import com.fqgj.common.utils.DES;
import com.fqgj.common.utils.DateUtil;
import com.fqgj.exception.common.ApplicationException;
import com.tj.bo.common.conf.enums.LoginTypeEnum;
import com.tj.bo.user.client.UserLoginService;
import com.tj.bo.user.client.enums.UserRealFlagEnum;
import com.tj.bo.user.client.vo.input.UserLogoutInput;
import com.tj.bo.user.client.vo.input.UserPasswordLoginInput;
import com.tj.bo.user.client.vo.input.UserSocialLoginInput;
import com.tj.bo.user.client.vo.output.UserLoginOutput;
import com.tj.bo.user.server.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * User: binary
 * Date: 2020/6/11
 * Time: 上午10:14
 */
@Service
public class UserLoginServiceImpl extends UserBaseService implements UserLoginService {

    @Override
    public UserLoginOutput passwordLogin(UserPasswordLoginInput userPasswordLoginInput) {
        if(userPasswordLoginInput.getLoginType() == null){
            userPasswordLoginInput.setLoginType(LoginTypeEnum.EMAIL.getCode());
        }
        UserAccessEntity userAccessEntityReal = userAccessDao.selectUserAccessByAppCodeAndEmail(userPasswordLoginInput.getAppCode(), UserRealFlagEnum.REAL.getType(), userPasswordLoginInput.getEmail());
        if (userAccessEntityReal == null) {
            throw new ApplicationException("Unregistered user, please register first.");
        }
        String desPassword = null;
        try {
            desPassword = DES.encrypt(userPasswordLoginInput.getPassword());
        } catch (Exception e) {
        }
        if (!Objects.equals(userPasswordLoginInput.getPassword(), userAccessEntityReal.getPassWord())
                && !Objects.equals(desPassword, userAccessEntityReal.getPassWord())) {
//            redisClient.incrBy(RedisKey.BINARY_OPTION_LOGIN_ERROR_PERMIT_TIMES, 1L, userPasswordLoginInput.getEmail(), DateUtil.getDate("yyyy-MM-dd"));
//            redisClient.expire(RedisKey.BINARY_OPTION_LOGIN_ERROR_PERMIT_TIMES, RedisTimeConstant.ONE_DAY, userPasswordLoginInput.getEmail(), DateUtil.getDate("yyyy-MM-dd"));
//            Long loginErrorTimes = redisClient.getNumber(RedisKey.BINARY_OPTION_LOGIN_ERROR_PERMIT_TIMES, userPasswordLoginInput.getEmail(), DateUtil.getDate("yyyy-MM-dd"));
//            if(loginErrorTimes >= 10L){
//                throw new ApplicationException("Wrong password, please check and re-enter," + (10L-loginErrorTimes) + "chances remained");
//            }
            throw new ApplicationException("Wrong password, please check and re-enter.");
        }
        userAccessEntityReal.setToken(UUID.randomUUID().toString().replaceAll("-", ""));
        userAccessEntityReal.setExpireDate(getExpireDate());
        userAccessDao.updateByPrimaryKey(userAccessEntityReal);

        UserAccessEntity userAccessEntityDemo = userAccessDao.selectUserAccessByCustomerCode(userAccessEntityReal.getAppCode(), UserRealFlagEnum.DEMO.getType(), userAccessEntityReal.getCustomerCode());
        userAccessEntityDemo.setToken(userAccessEntityReal.getToken());
        userAccessDao.updateByPrimaryKey(userAccessEntityDemo);

        UserBehaviorDeviceEntity userBehaviorDeviceEntity = userBehaviorDeviceDao.selectUserBehaviorDeviceByUserCode(userAccessEntityReal.getUserCode());
        if (userBehaviorDeviceEntity == null) {
            userBehaviorDeviceEntity = new UserBehaviorDeviceEntity();
            userBehaviorDeviceEntity.setUserCode(userAccessEntityReal.getUserCode());
            userBehaviorDeviceEntity.setDeviceId(userPasswordLoginInput.getDeviceId());
            userBehaviorDeviceEntity.setClientId(userPasswordLoginInput.getClientId());
            userBehaviorDeviceEntity.setPlatform(userPasswordLoginInput.getPlatform());
            userBehaviorDeviceEntity.setVersion(userPasswordLoginInput.getVersion());
            userBehaviorDeviceEntity.setVersionCode(userPasswordLoginInput.getVersionCode());
            userBehaviorDeviceDao.insert(userBehaviorDeviceEntity);
        } else {
            userBehaviorDeviceEntity.setDeviceId(userPasswordLoginInput.getDeviceId());
            userBehaviorDeviceEntity.setClientId(userPasswordLoginInput.getClientId());
            userBehaviorDeviceEntity.setPlatform(userPasswordLoginInput.getPlatform());
            userBehaviorDeviceEntity.setVersion(userPasswordLoginInput.getVersion());
            userBehaviorDeviceEntity.setVersionCode(userPasswordLoginInput.getVersionCode());
            userBehaviorDeviceDao.updateByPrimaryKey(userBehaviorDeviceEntity);
        }

        UserBehaviorLoginEntity userBehaviorLoginEntity = new UserBehaviorLoginEntity();
        userBehaviorLoginEntity.setUserCode(userAccessEntityReal.getUserCode());
        userBehaviorLoginEntity.setDeviceId(userPasswordLoginInput.getDeviceId());
        userBehaviorLoginEntity.setClientId(userPasswordLoginInput.getClientId());
        userBehaviorLoginEntity.setPlatform(userPasswordLoginInput.getPlatform());
        userBehaviorLoginEntity.setVersion(userPasswordLoginInput.getVersion());
        userBehaviorLoginEntity.setVersionCode(userPasswordLoginInput.getVersionCode());
        userBehaviorLoginEntity.setChannel(userPasswordLoginInput.getChannel());
        userBehaviorLoginEntity.setAppCode(userPasswordLoginInput.getAppCode());
        userBehaviorLoginEntity.setPackageName(userPasswordLoginInput.getPackageName());
        userBehaviorLoginEntity.setIp(userPasswordLoginInput.getIp());
        userBehaviorLoginDao.insert(userBehaviorLoginEntity);

        return new UserLoginOutput().setRealToken(userAccessEntityReal.getToken()).setDemoToken(userAccessEntityDemo.getToken());
    }

    @Override
    public UserLoginOutput socialLogin(UserSocialLoginInput userSocialLoginInput) {
        UserThirdPartEntity userThirdPartEntity = userThirdPartDao.selectUserThirdPartByOpenId(userSocialLoginInput.getUserRegisterTypeEnum().getType(), userSocialLoginInput.getOpenId());
        if (userThirdPartEntity == null) {
            throw new ApplicationException("Third party account not registered, please check and login.");
        }
        UserAccessEntity userAccessEntityReal = userAccessDao.selectUserAccessByUserCode(userThirdPartEntity.getUserCode());
        userAccessEntityReal.setToken(UUID.randomUUID().toString().replaceAll("-", ""));
        userAccessEntityReal.setExpireDate(getExpireDate());
        userAccessDao.updateByPrimaryKey(userAccessEntityReal);

        UserAccessEntity userAccessEntityDemo = userAccessDao.selectUserAccessByCustomerCode(userAccessEntityReal.getAppCode(), UserRealFlagEnum.DEMO.getType(), userAccessEntityReal.getCustomerCode());
        userAccessEntityDemo.setToken(userAccessEntityReal.getToken());
        userAccessDao.updateByPrimaryKey(userAccessEntityDemo);

        UserBehaviorDeviceEntity userBehaviorDeviceEntity = userBehaviorDeviceDao.selectUserBehaviorDeviceByUserCode(userAccessEntityReal.getUserCode());
        if (userBehaviorDeviceEntity == null) {
            userBehaviorDeviceEntity = new UserBehaviorDeviceEntity();
            userBehaviorDeviceEntity.setUserCode(userAccessEntityReal.getUserCode());
            userBehaviorDeviceEntity.setDeviceId(userSocialLoginInput.getDeviceId());
            userBehaviorDeviceEntity.setClientId(userSocialLoginInput.getClientId());
            userBehaviorDeviceEntity.setPlatform(userSocialLoginInput.getPlatform());
            userBehaviorDeviceEntity.setVersion(userSocialLoginInput.getVersion());
            userBehaviorDeviceEntity.setVersionCode(userSocialLoginInput.getVersionCode());
            userBehaviorDeviceDao.insert(userBehaviorDeviceEntity);
        } else {
            userBehaviorDeviceEntity.setDeviceId(userSocialLoginInput.getDeviceId());
            userBehaviorDeviceEntity.setClientId(userSocialLoginInput.getClientId());
            userBehaviorDeviceEntity.setPlatform(userSocialLoginInput.getPlatform());
            userBehaviorDeviceEntity.setVersion(userSocialLoginInput.getVersion());
            userBehaviorDeviceEntity.setVersionCode(userSocialLoginInput.getVersionCode());
            userBehaviorDeviceDao.updateByPrimaryKey(userBehaviorDeviceEntity);
        }

        UserBehaviorLoginEntity userBehaviorLoginEntity = new UserBehaviorLoginEntity();
        userBehaviorLoginEntity.setUserCode(userAccessEntityReal.getUserCode());
        userBehaviorLoginEntity.setDeviceId(userSocialLoginInput.getDeviceId());
        userBehaviorLoginEntity.setClientId(userSocialLoginInput.getClientId());
        userBehaviorLoginEntity.setPlatform(userSocialLoginInput.getPlatform());
        userBehaviorLoginEntity.setVersion(userSocialLoginInput.getVersion());
        userBehaviorLoginEntity.setVersionCode(userSocialLoginInput.getVersionCode());
        userBehaviorLoginEntity.setChannel(userSocialLoginInput.getChannel());
        userBehaviorLoginEntity.setAppCode(userSocialLoginInput.getAppCode());
        userBehaviorLoginEntity.setPackageName(userSocialLoginInput.getPackageName());
        userBehaviorLoginEntity.setIp(userSocialLoginInput.getIp());
        userBehaviorLoginDao.insert(userBehaviorLoginEntity);

        return new UserLoginOutput().setRealToken(userAccessEntityReal.getToken()).setDemoToken(userAccessEntityDemo.getToken());
    }

    @Override
    public void userLogout(UserLogoutInput userLogoutInput) {
        UserBehaviorLogoutEntity entity = new UserBehaviorLogoutEntity();
        entity.setUserCode(userLogoutInput.getUserCode());
        entity.setDeviceId(userLogoutInput.getDeviceId());
        entity.setClientId(userLogoutInput.getClientId());
        entity.setPlatform(userLogoutInput.getPlatform());
        entity.setVersion(userLogoutInput.getVersion());
        entity.setVersionCode(userLogoutInput.getVersionCode());
        entity.setChannel(userLogoutInput.getChannel());
        entity.setAppCode(userLogoutInput.getAppCode());
        entity.setPackageName(userLogoutInput.getPackageName());
        entity.setIp(userLogoutInput.getIp());
        userBehaviorLogoutDao.insert(entity);
    }

    private Date getExpireDate() {
        return DateUtil.addDate(new Date(), 30);
    }
}
